window._FIGHTPREPARE = {

    getDropTalk:function(armyArea){
        var areaConfig = _AREAARMY[armyArea];
        if(areaConfig){

            var result = [];

            var aniboss = areaConfig.animals;
            for(var i=0;i<aniboss.length;i++){
                if(_DROP[aniboss[i]]){
                    result.push(aniboss[i]+"会掉落:"+_DROP[aniboss[i]].join(','));
                }
            }

            var yeboss = areaConfig.boss;
            for(var i=0;i<yeboss.length;i++){
                if(_SDROP[yeboss[i]]){
                    result.push(yeboss[i]+"会掉落:"+_SDROP[yeboss[i]].join(','));
                }
            }

            return result;
        }

        return ['此地没有掉落情报'];
    },

    getFightIntro:function(armyArea,areaType){
        var areaConfig = _AREAARMY[armyArea];
        if(areaConfig){
            var master = _MASTERHELPER.getMasterOfCity(armyArea);
			if(!master){
				master='无';
			}
            //var npcJob = _MASTERHELPER.getJobsOfCity(armyArea);
            //var result = "地名:"+armyArea+"\n";
            //result+= "地形:"+areaType+"\n";
            //result+= "所属:"+master+"\n";
            //result+= "等级:"+areaConfig.lv+"\n";
            var result="["+master+"]["+armyArea+"]["+(areaType?areaType:'平原')+"][lv"+areaConfig.lv+"]";
			//if(npcJob&&npcJob.length>0){
			//	result+= "\n职业:"+npcJob.join(',');
			//}
            result+= "\n野兽:";
            var aniboss = areaConfig.animals;
            for(var i=0;i<aniboss.length;i++){
                var bossConfig = _NPC[aniboss[i]];
                if(bossConfig){
                    if(bossConfig.ftype){
                        result+=aniboss[i]+"("+bossConfig.ftype+")"+bossConfig.lv+" ";
                    }else{
                        result+=aniboss[i]+" ";
                    }
                }
            }

            result+= "\n武将:";
            var yeboss = areaConfig.boss;
            for(var i=0;i<yeboss.length;i++){
                var bossConfig = _NPC[yeboss[i]];
                if(bossConfig){
                    result+=yeboss[i]+"("+bossConfig.ftype+")"+bossConfig.lv+" ";
                }
            }
            
            /*
            result+= "\n城将:";
            var boss = areaConfig.guard;
            for(var i=0;i<boss.length;i++){
                var bossConfig = _NPC[boss[i]];
                if(bossConfig){
                    result+=boss[i]+"("+bossConfig.ftype+")"+bossConfig.lv+" ";
                }
            }
            */
            return result;
        }else{
            return "未知";
        }
        
    },

    //创建隐藏战斗
    initHideFight:function(story){

        var storyConf = _HSTORY[story];
        var playerMap = storyConf.player;
        var armyMap = storyConf.army;

        var armyArr = [];
        var playerArr = [];

        for(var i=0;i<window.gameData.playerTeam.length;i++){
			
			var realName = window.gameData.playerTeam[i].isMainRole?"主角":window.gameData.playerTeam[i].name;
			
            if(!playerMap||_CU.isContain(playerMap,realName)){
                playerArr.push(window.gameData.playerTeam[i]);
            }
        }

        for(var i=0;i<armyMap.length;i++){
            if(_HDLVHELPER.getHdLv()==0){
                armyArr.push(_ROF.createRole(armyMap[i]));
            }else{
                armyArr.push(_ROF.createRoleAndSetPlayerLv(armyMap[i]));
            }
        }

        _FIGHTHELPER.prepareFight(playerArr,armyArr,
            {
                type:'hideStory',
                story:story
            });
        //_MAP.jumpToFight();
        _MAP.jumpToFightNoAutoSave();
    },

    //创建城战
    initCityFight:function(cityName,target){

        //console.log('城战:',cityName,target);

        if(
            _AREAARMY[cityName]
            &&_AREAARMY[cityName].guard
            &&_AREAARMY[cityName].guard.length>0
        ){

            //console.log('城战守卫:',_AREAARMY[cityName].guard);

            var cityMaster =  window.gameData.cityLog[cityName];

            //console.log('城战势力:',cityMaster);

            var monsterArr = [];
            for(var i=0;i<_AREAARMY[cityName].guard.length;i++){
                var bossname = _AREAARMY[cityName].guard[i];
                var bosslog = _GAMEDATAHELPER.getNpcLog(bossname);

                //console.log('城战人员信息:',bosslog);

                if(
                  bosslog
                  &&bosslog.country==cityMaster
                  &&_GAMEDATAHELPER.canNpcFight(bossname)
                ){
                    //monsterArr.push(_ROF.createRole(bossname));
                    monsterArr.push(_ROF.createRoleAndSetPlayerLv(bossname));
                    if(monsterArr.length>=5){
                        break;
                    }
                }
            }

            //守城人数少于5人可上阵替补
            if(monsterArr.length<5){  
                _FIGHTPREPARE.getBossFoCityFight(monsterArr,cityMaster,cityName);
            }

            //console.log('城战上阵:',monsterArr);
            if(monsterArr.length>0){
                _FIGHTHELPER.prepareFight(window.gameData.playerTeam,monsterArr,
                                        {
                                            type:'cityFight',
                                            cityName:cityName,
                                            citytarget:target
                                        });
                _MAP.jumpToFight();
                return;
            }
        }

        window.gameData.cityLog[cityName]=window.gameData.country;
        _MAP.jumpToMap(cityName,target);
        
    },

    //城战替补
    //先找该势力能参战且等级小于原守将平均等级的武将
    //再找小兵替补
    getBossFoCityFight:function(monsterArr,master,armyArea){
        var areaConfig = _AREAARMY[armyArea];
        var guardarr = areaConfig.guard;
        var tmpbossarr = [];
        var tmpbossarrlv = [];
        var neednmb = 5-monsterArr.length;
        var totallv = 0;
        var avalv = 0;
        var alreadyMap = {};

        for(var i=0;i<monsterArr.length;i++){
            alreadyMap[monsterArr[i].name]=1;
        }

        if(guardarr&&guardarr.length>0){
            for(var i=0;i<guardarr.length;i++){
                var tmpguard = _NPC[guardarr[i]];
                totallv+=tmpguard.lv;
            }
            avalv=Math.floor(totallv/guardarr.length);
        }

        for(var key in _NPC){
            var npclog = _GAMEDATAHELPER.getNpcLog(key);
            if(
                npclog
                &&!alreadyMap[key]
                &&npclog.country==master
                &&_GAMEDATAHELPER.canNpcFight(key)
            ){
                tmpbossarr.push(key);
                if(_NPC[key].lv<=avalv){
                    tmpbossarrlv.push(key);
                }
            }
        }
        if(neednmb>=5){
            for(var i=0;i<neednmb;i++){
                if(tmpbossarr.length>0){
                    var randombossIndex = _CU.random(tmpbossarr.length);
                    var randomboss = tmpbossarr[randombossIndex];
                    tmpbossarr.splice(randombossIndex,1);
                    monsterArr.push(_ROF.createRoleAndSetPlayerLv(randomboss));
                }

            }
        }else{
            for(var i=0;i<neednmb;i++){
                if(tmpbossarrlv.length>0){
                    var randombossIndex = _CU.random(tmpbossarrlv.length);
                    var randomboss = tmpbossarrlv[randombossIndex];
                    tmpbossarrlv.splice(randombossIndex,1);
                    monsterArr.push(_ROF.createRoleAndSetPlayerLv(randomboss));
                }

            }
        }

        if(monsterArr.length<5){
            _FIGHTPREPARE.initNpcMonster(monsterArr,armyArea,'土',_GAMEDATAHELPER.getTeamLv());
        }
    },

    //创建地图boss战
    initMapBossFight:function(bossName,map,x,y){

        var monsterArr = [];
        if(
            _NPC[bossName]
            &&_MFR.animalJob[_NPC[bossName].job]
        ){
            var tempmonster = _ROF.createAnimal(bossName,window._NPC[bossName].lv,{isboss:true});
            monsterArr.push(tempmonster);
        }else{
            var tempmonster = _ROF.createRoleAndSetPlayerLv(bossName);
            monsterArr.push(tempmonster);
        }

        if(monsterArr.length>0){
            _FIGHTHELPER.prepareFight(window.gameData.playerTeam,monsterArr,
                                        {
                                        type:'normalFight',
                                        bossFightMap:map,
                                        bossFightx:x,
                                        bossFighty:y
                                        });
            _MAP.jumpToFight();
            return true;
        }

        return false;

    },


    //创建宝箱神兽战斗
    initBoxFight:function(){

        var monsterArr =[];

        var bossName = _CU.randomObj(_CS.godnpc);

        var tempmonster = _ROF.createAnimal(bossName,window._NPC[bossName].lv,{isboss:true});

        monsterArr.push(tempmonster);

        if(monsterArr.length>0){
            _FIGHTHELPER.prepareFight(window.gameData.playerTeam,monsterArr,{type:'normalFight'});
            _MAP.jumpToFight();
            return true;
        }

        return false;
    },

    //创建埋伏战
    initFujiFight:function(cityName){

        //console.log('埋伏战:',cityName);

        if(_AREAARMY[cityName]){

            if(window.gameData.fujiplace==cityName){//同一个城不会连续触发伏击
                return false;
            }
            window.gameData.fujiplace=cityName;

            var npcmap = {};
            var guardArr = _AREAARMY[cityName].guard;
            var bossArr = _AREAARMY[cityName].boss;

            for(var i=0;i<guardArr.length;i++){
                var npcname = guardArr[i];
                var npclog = _GAMEDATAHELPER.getNpcLog(npcname);
                if(
                    !npcmap[npcname]
                    &&(npclog.country=='中立'||npclog.country!=window.gameData.country)
                    &&_GAMEDATAHELPER.canNpcFight(npcname)
                ){
                    npcmap[npcname]=1;
                }
            }

            for(var i=0;i<bossArr.length;i++){
                var npcname = bossArr[i];
                var npclog = _GAMEDATAHELPER.getNpcLog(npcname);
                if(
                    !npcmap[npcname]
                    &&(npclog.country=='中立'||npclog.country!=window.gameData.country)
                    &&_GAMEDATAHELPER.canNpcFight(npcname)
                ){
                    npcmap[npcname];
                }
            }

            //console.log('埋伏战2:',npcmap);
            
            var monsterArr = [];
            for(var key in npcmap){
                if(monsterArr.length<5){
                    //monsterArr.push(key);
                    //monsterArr.push(_ROF.createRole(key));

                    if(_HDLVHELPER.getHdLv()==0){
                        monsterArr.push(_ROF.createRole(key));
                    }else{
                        monsterArr.push(_ROF.createRoleAndSetPlayerLv(key));
                    }

                }
            }

            //console.log('埋伏战3:',monsterArr);

            if(monsterArr.length>0){
                _FIGHTHELPER.prepareFight([window.gameData.playerTeam[0]],monsterArr,{type:'normalFight'});
                //_MAP.jumpToFight();
                return true;
            }
            
        }


        return false;


    },

    //创建野外战斗
    initFight:function(armyArea,areaType,fightType){

        //console.log(armyArea,areaType);

        if(_AREAARMY[armyArea]){

            var citymaster = _GAMEDATAHELPER.getCityCountry(armyArea);
            var areaConfig = _AREAARMY[armyArea];
            var monsterArr = [];
            var bossFtype = '土';
            var ispeopleOnly = false;
            var hasBoss = false;
            if(_MAPTYPE[areaType]){
                bossFtype = _MAPTYPE[areaType];
            }

            if(fightType){
                hasBoss=true;
                if(fightType=="peopleFight"){
                    ispeopleOnly=true;
                }else if(fightType=="animalFight"){
                    ispeopleOnly=false;
                }
            }else{

                ispeopleOnly =  window.gameData.country
                                &&citymaster
                                &&window.gameData.country!=citymaster
                                &&(Math.random()>_CS.animalper);

            }

            if(ispeopleOnly){//人形怪

                _FIGHTPREPARE.initBossMonster(areaConfig,monsterArr,bossFtype,hasBoss);

                if(!_FIGHTPREPARE.initNpcMonster(monsterArr,armyArea,bossFtype,areaConfig.lv)){
                    _FIGHTPREPARE.initAnimalMonster(areaConfig,monsterArr,bossFtype,hasBoss);
                };

            }else{//野兽怪

                if(!ispeopleOnly){
                    _FIGHTPREPARE.initAnimalMonster(areaConfig,monsterArr,bossFtype,hasBoss);
                }

            }

            if(monsterArr.length>0){
                _FIGHTHELPER.prepareFight(window.gameData.playerTeam,monsterArr,{type:'normalFight'});
                _MAP.jumpToFight();
                return true;
            }
        }

        return false;
    },

    initAnimalMonster:function(areaConfig,monsterArr,bossFtype,hasBoss){

        var animals = areaConfig.animals;
        if(animals){

            var npcnmb = window.gameData.playerTeam.length;
            var enableboss = [];
            var enableNormal = [];

            //先根据地形判断能出的怪
            for(var i=0;i<animals.length;i++){
                if(window._NPC[animals[i]]&&
                    (
                        !window._NPC[animals[i]].ftype
                        ||window._NPC[animals[i]].ftype==bossFtype
                    )
                ){

                    if(window._NPC[animals[i]].ftype){
                        enableboss.push(animals[i]);
                    }else{
                        enableNormal.push(animals[i]);
                    }
                }
            }

            //判断是否出精英怪
            if(
                enableboss.length>0
                &&(Math.random()<_CS.bossper||hasBoss)
            ){
                var bossName = _CU.randomObj(enableboss);
                var tempmonster = _ROF.createAnimal(bossName,window._NPC[bossName].lv,{isboss:true});
                monsterArr.push(tempmonster);
            }

            //判断是否出普通怪
            npcnmb-=monsterArr.length;
            if(npcnmb>0&&enableNormal.length>0){
                var attr = window.gameData.armybaseAttr+Math.floor(50*areaConfig.lv/1000);
                for(var i=0;i<npcnmb;i++){
                    var bossName = _CU.randomObj(enableNormal);
                    var tempmonster = _ROF.createAnimal(bossName,areaConfig.lv,{ftype:bossFtype,attr:attr});
                    monsterArr.push(tempmonster);
                }
            }
            
        }

    },

    initBossMonster:function(areaConfig,monsterArr,bossFtype,hasBoss){

        var boss = areaConfig.boss;

        if(boss){
            var enableboss = [];

            for(var i=0;i<boss.length;i++){
                if(window._NPC[boss[i]]
                    &&window._NPC[boss[i]].ftype==bossFtype
                    &&_GAMEDATAHELPER.canNpcFight(boss[i])){
                    enableboss.push(boss[i]);
                }
            }

            if(
                enableboss.length>0
                &&(Math.random()<_CS.bossper||hasBoss)
            ){
                var bossName = _CU.randomObj(enableboss);
                monsterArr.push(_ROF.createRole(bossName));
            }
        }

    },

    initNpcMonster:function(monsterArr,armyArea,ftype,lv){
        var npcnmb = window.gameData.playerTeam.length-monsterArr.length;
        if(npcnmb>0){
            var master = _MASTERHELPER.getMasterOfCity(armyArea);
            if(master){
                var npcJob = _MASTERHELPER.getJobsOfCity(armyArea);
                var attr = window.gameData.armybaseAttr+Math.floor(50*lv/1000);
                for(var i=0;i<npcnmb;i++){
                    var randomJob = _CU.randomObj(npcJob);
                    var npcname = master?master+randomJob:randomJob;
                    var tmpnpc = _ROF.createJobRole(randomJob,
                                {ftype:ftype,equiplv:'C',attr:attr,lv:lv,npcname:npcname,flag:_MASTER[master].flag});
                    if(tmpnpc!=null){
                        monsterArr.push(tmpnpc);
                    }
                }
                return true;
            }
        }

        return false;

    }


}